package com.sky.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.admin.CategoryMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.service.admin.CategoryService;
import com.sky.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description CategoryServiceImpl
 * @Author shejunlian
 * @Date 2024-07-22
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */
    @Override
    public void addClassify(CategoryDTO categoryDTO) {
        //利用糊涂工具类 将categoryDTO 转化为 category
        Category category = BeanUtil.toBean(categoryDTO, Category.class);
        //补充字段 设置时间和操作人
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        //分类状态 默认为禁用状态0
        category.setStatus(StatusConstant.DISABLE);
        //调用Mapper
        categoryMapper.addClassify(category);

    }

    /**
     * 分类分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageClassify(CategoryPageQueryDTO categoryPageQueryDTO) {
        //设置PageHelper插件
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        //下一条sql进行分页，自动加入limit关键字分页
        Page<Category> page = categoryMapper.pageClassify(categoryPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @Override
    public void deleteClassify(Long id) {
        //查询判断是否关联了菜品, 如果关联了就抛出业务异常
        Integer count = dishMapper.countByCategoryId(id);
        if (count > 0) {
            //分页有菜品, 不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //查询判断是否关联了套餐, 如果关联了就抛出异常
        count = setmealMapper.countByCategoryId(id);
        if (count > 0) {
            //当前分页有套餐, 不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //调用Mapper 删除分页数据
        categoryMapper.deleteClassify(id);

    }

    /**
     * 修改分页
     * @param categoryDTO
     * @return
     */
    @Override
    public void updateClassify(CategoryDTO categoryDTO) {
        //转换类型
        Category category = BeanUtil.toBean(categoryDTO, Category.class);
        //设置修改时间和修改人
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        //调用Mapper
        categoryMapper.update(category);
    }

    /**
     * 启用禁用分类
     * @param status
     * @param id
     * @return
     */
    @Override
    public void statusClassify(Integer status, Long id) {
        // 使用构建器创建实体类对象 并且设置属性值(状态, id, 修改时间, 修改人)
        Category category = Category.builder()
                .status(status)
                .id(id)
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();
        // 调用Mapper启用禁用分类
        categoryMapper.update(category);

    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<CategoryVo> insertClassify(Integer type) {
        List<CategoryVo> voList = categoryMapper.insertClassify(type);
        return voList;
    }
}
